package ph.com.gs3.sockettest.Manager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import android.os.AsyncTask;
import android.util.Log;

public class SocketCommunicatorModule {

	public static final String TAG = SocketCommunicatorModule.class.getSimpleName();

	private String host = "192.168.0.101";
	private int port = 3013;

	public void connect() {

		AsyncTask <Void, Void, Void> connectionTask = new AsyncTask<Void, Void, Void>() {

			@Override
            protected Void doInBackground(Void... params) {
				Socket socket = null;
				try {
				
					
					socket = new Socket(host, port);
					PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
					BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

					String userInput;
					while ((userInput = stdIn.readLine()) != null) {
						out.println(userInput);
						Log.v(TAG, in.readLine());
					}

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					if (socket != null) {
						try {
							socket.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
	            return null;
            }
			
		};
		
		connectionTask.execute();		

	}
}
